<template>
  <div class="divBox">
    <el-card class="box-card">
      <el-form inline size="small" @submit.native.prevent>
        <el-form-item label="关键词：">
          <el-input v-model="listPram.unitName" placeholder="请输入单位名称" clearable class="selWidth"/>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" size="mini" type="primary" @click="handleSearch">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
        <el-form-item class="el-form-item-align-right">
          <el-button icon="el-icon-circle-plus" size="mini" type="primary" @click="handlerOpenEdit(0)" v-hasPermi="['admin:department:save']">添加单位</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="listData.list"  v-loading="loading" size="mini" :header-cell-style=" {fontWeight:'bold',background:'#f8f8f9',color: '#515a6e',height:'40px'}" border>
        <el-table-column
          prop="id"
          label="ID"
          width="50"
        />
        <el-table-column label="单位名称" prop="unitName" min-width="120"/>
        <el-table-column label="分类" prop="typeText" min-width="120"/>
        <el-table-column label="排序" prop="sort" min-width="120" />
        <el-table-column label="操作" min-width="130" align="center" fixed="right">
          <template slot-scope="scope">
            <el-button icon="el-icon-edit" type="primary" size="mini" @click="handlerOpenEdit(1,scope.row)" v-hasPermi="['admin:department:info']">编辑</el-button>
            <el-button icon="el-icon-delete" type="danger" size="mini" @click="handlerOpenDel(scope.row)" v-hasPermi="['admin:department:delete']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="listPram.page"
        :page-sizes="constants.page.limit"
        :layout="constants.page.layout"
        :total="listData.count"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <el-dialog
      :visible.sync="editDialogConfig.visible"
      :title="editDialogConfig.isCreate === 0? '创建单位':'编辑单位'"
      destroy-on-close
      :close-on-click-modal="false"
      width="700px"
    >
      <edit
        v-if="editDialogConfig.visible"
        :is-create="editDialogConfig.isCreate"
        :edit-data="editDialogConfig.editData"
        :all-type-list="typeList"
        @hideEditDialog="hideEditDialog"
      />
    </el-dialog>
  </div>
</template>

<script>
import * as departmentApi from '@/api/department.js'
import * as roleApi from '@/api/role.js'
import edit from './edit'
import { checkPermi } from "@/utils/permission";
import * as selfUtil from "@/utils/ZBKJIutil";
import * as categoryApi from "@/api/categoryApi";
export default {
  // name: "index"
  components: { edit },
  data() {
    return {
      constants:this.$constants,
      loading:false,
      listData: { list: [] },
      listPram: {
        unitName: '',
        /*departmentId: null,
        ipAddress: null,
        macAddress: null,
        personId: null,*/
        page: 1,
        limit: this.$constants.page.limit[0]
      },
      editDialogConfig: {
        visible: false,
        isCreate: 0, // 0=创建，1=编辑
        editData: {}
      },

      typeList:[],
    }
  },
  mounted() {
    this.handleGetDepartmentList()
    this.initDepartmentTypeList()
  },
  methods: {
    checkPermi,
    handleSearch() {
      this.listPram.page = 1
      this.handleGetDepartmentList()
    },
    handleSizeChange(val) {
      this.listPram.limit = val
      this.handleGetDepartmentList()
    },
    handleCurrentChange(val) {
      this.listPram.page = val
      this.handleGetDepartmentList()
    },
    handlerOpenDel(rowData) {
      this.$confirm('确认删除当前数据').then(() => {
        const _pram = { id: rowData.id}
        departmentApi.depDel(_pram).then(data => {
          this.$message.success('删除数据成功')
          this.handleGetDepartmentList()
        })
      })
    },
    initDepartmentTypeList(){
      this.loading = true

      categoryApi.categoryDepartmentTypeList().then(data => {
        //let data = this.constants.staticDepartmentTypeList
        this.typeList = data
        this.loading = false
      }).catch(()=>{
        this.loading = false
      })
    },

    handleGetDepartmentList() {
      departmentApi.getList( this.listPram ).then(data => {
        /*let data = {
          count: this.constants.staticDepartmentList.length,
          list:this.constants.staticDepartmentList
        }*/
        this.listData.count = data.count
        this.listData.list = data.list
      })
    },
    handlerOpenEdit(isCreate, editDate) {
      this.editDialogConfig.editData = editDate
      this.editDialogConfig.isCreate = isCreate
      this.editDialogConfig.visible = true
    },
    hideEditDialog() {
      this.editDialogConfig.visible = false
      this.handleGetDepartmentList()
    },
    resetQuery(){
      this.listPram.unitName = '';
      this.handleGetDepartmentList()
    }
  }
}
</script>

<style scoped>

</style>
